//
//  STNavigationBar.swift
//  iPhone
//
//  Created by Toj on 11/2/22.
//

import UIKit
import TTCOM_iOS

private let STNavigationHeight = CGFloat.nav_barHeight
private let STItemDefaultSize = CGSize(width: CGFloat.nav_barHeight, height: CGFloat.nav_barHeight)

class STNavigationBar: UIView {
    
    private(set) var backgroundImageView: UIImageView = {
        let view = UIImageView()
        return view
    }()

    private(set) var contentView: UIView = {
        let view = UIView()
        return view
    }()

    var leftBarView: UIView? { willSet {
        leftBarView?.removeFromSuperview()
        guard let leftView = newValue else { return }
        contentView.addSubview(leftView)
    } }
    
    var rightBarView: UIView? { willSet {
        rightBarView?.removeFromSuperview()
        guard let rightView = newValue else { return }
        contentView.addSubview(rightView)
    } }
    
    var leftBarViews: [UIView]? { willSet {
        leftBarViews?.forEach({ (view) in
            view.removeFromSuperview()
        })
        
        newValue?.forEach({ (view) in
            contentView.addSubview(view)
        })
        let count = newValue?.count ?? 0
        leftBarView?.isHidden = count != 0
    } }
    
    var rightBarViews: [UIView]? { willSet {
        rightBarViews?.forEach({ view in
            view.removeFromSuperview()
        })
        
        newValue?.forEach({ (view) in
            contentView.addSubview(view)
        })
        let count = newValue?.count ?? 0
        rightBarView?.isHidden = count != 0
    } }
    
    var title: String = "" { didSet {
        guard let titleView = titleView as? UILabel else { return }
        titleView.text = title
    } }
    
    var titleView: UIView? = {
        let view = UILabel()
        view.textColor = .white
        view.textAlignment = .center
        return view
    }() {
        willSet {
            titleView?.removeFromSuperview()
            guard let titleView = newValue else { return }
            contentView.addSubview(titleView)
        }
    }
    
    @objc private func didTapBack() {
        UIViewController.vc_currentNavigationTopViewController?.navigationController?.popViewController(animated: true)
    }
    
    lazy var backButton: UIButton = {
        let view = UIButton()
        view.addTarget(self, action: #selector(didTapBack), for: .touchUpInside)
        let image = "com_nav_back_white".img_resource
        view.btn_setImage = image
        view.frame.size = image?.size ?? .zero
        return view
    }()
    
    private var lrMargin: CGFloat {
        return CGFloat.dv_safeAreaLeft
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
        isHidden = true
        backgroundColor = .random
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupMakeAddSubviews()
        isHidden = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)
        if hitView == self {
            return nil
        }
        return hitView
    }
}

extension STNavigationBar {
    
    private func setupMakeAddSubviews() {
        
        addSubview(backgroundImageView)
        addSubview(contentView)
      
        leftBarView = backButton
        if let view = leftBarView {
            contentView.addSubview(view)
        }
        if let view = titleView {
            contentView.addSubview(view)
        }
    }
    
    private func setupMakeLayoutSubviews() {
        layoutIfNeeded()
        
        backgroundImageView.snp.remakeConstraints { make in
            make.edges.equalTo(self)
        }
        contentView.snp.remakeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(STNavigationHeight)
        }
        if let titleView = titleView {
            let size = titleView.frame.size
            titleView.snp.remakeConstraints({ (make) in
                make.center.equalTo(contentView)
                guard !titleView.isKind(of: UILabel.self) else { return }
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            })
        }
        
        var left = lrMargin
        if let leftBarView = leftBarView {
            var size = leftBarView.frame.size
            if size == .zero { size = STItemDefaultSize }
            
            leftBarView.snp.remakeConstraints({ (make) in
                make.left.equalTo(left)
                make.centerY.equalTo(contentView)
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            })
        }
        
        if let rightBarView = rightBarView {
            var size = rightBarView.frame.size
            if size == .zero { size = STItemDefaultSize }
            
            rightBarView.snp.remakeConstraints({ (make) in
                make.right.equalTo(snp.right).offset(-left)
                make.centerY.equalTo(contentView)
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            })
        }
        
        var size: CGSize = .zero
        let margin: CGFloat = 25
        leftBarViews?.forEach({ (view) in
            size = view.frame.size
            if size == .zero { size = STItemDefaultSize }
            
            view.snp.remakeConstraints { (make) in
                make.left.equalTo(left)
                make.centerY.equalTo(contentView)
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            left = left + size.width + margin
        })
        
        var right = lrMargin
        rightBarViews?.reversed().forEach({ (view) in
            size = view.frame.size
            if size == .zero { size = STItemDefaultSize }
            
            view.snp.remakeConstraints { (make) in
                make.right.equalTo(snp.right).offset(-right)
                make.centerY.equalTo(contentView)
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            right = right + size.width + margin
        })
    }
}
